home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Learn Microsoft Visual Basic 6.0 Now
/
Learn Microsoft Visual Basic 6.0 Now (Microsoft Press)(X03-58607)(1998).ISO
/
media
/
chap07
/
b07d010.cc2
< prev
next >
Wrap
Text File
|
1998-06-07
|
3KB
|
57 lines
0, An error handler is a routine that helps
3, your Visual Basic program continue when
5, it encounters a runtime error. In this
8, demonstration I'll create an error
9, handler that recovers from runtime errors
11, associated with floppy disk drive problems.
14, The program I'm running now loads a
16, Windows metafile named Printout 2 from
18, Drive A when I click the Check Drive button.
22, However, it currently generates a
23, runtime error if a disk is not in the drive,
26, a common problem. To trap the
32, show-stopping error, I'll close the program and
35, double-click the Check Drive button to
37, create an error handler in the event
40, procedure that uses the LoadPicture function
44, to open the Windows metafile. At the top
47, of the event procedure, I'll type the
50, statement On Error, Go To Disk Error, the
57, name of my error handler. And at the
60, bottom of the event procedure, I'll add the
62, program code for the error handler. The
69, conditional expression in this error
71, handler's If...Then statement tests the
73, Number property of the error object to see
76, if it contains the number 71, the error
79, code that was returned whenever a disk
81, drive is not functioning. If a disk
83, error has occurred, the program gives the
85, user the opportunity to fix the problem
88, either by closing the drive latch or by
90, inserting a new disk. And then, the
93, program resumes. If the error has not been
98, related to the disk drive, the program
100, assumes that the disk was valid but that the
103, file could not be located in the root
105, folder. This program logic is executed by
108, the Else clause. Then, the error
111, handler branches to the Stop Trying label at
115, the bottom of the procedure. In either
117, case, the error handler prints a message
119, for the user and stops the program from
122, being prematurely terminated. Now, let's
125, run the program again. Now when I click
132, the Check Drive button, I receive a
134, dialog box that says, "Disk not ready" that
137, asks me to close the drive latch. This
139, comes right from my error handler. Now
142, I'll take a moment to put the disk in
144, Drive A with the appropriate file and click
146, the OK button. After a few moments, the
150, Printout2.wmf is displayed on the form.
154, You can use this same technique to add
156, error handling support to any Visual
158, Basic program. Just change the error numbers
161, and the messages.
162, END